<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/jsp/common/taglibs.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login</title>
<script src="<%=request.getContextPath()%>/js/jquery.min.js"
		type="text/javascript"></script>
<script src="<%=request.getContextPath()%>/js/jquery.md5.js"
		type="text/javascript"></script>
<script src="<%=request.getContextPath()%>/js/validation.js"
		type="text/javascript"></script>
<script>
	 var contextPath = "<%=request.getContextPath()%>";
	 var emailVerify,passwordVerify;
	 $(function(){
		    emailVerify = new LiveValidation('email' , {onlyOnBlur: true});
			emailVerify.add(Validate.Email, { failureMessage: "请输入正确的电子邮箱地址"} );
			emailVerify.add(Validate.Presence, { failureMessage: "请输入正确的电子邮箱地址"}  );
			passwordVerify = new LiveValidation('password', {onlyOnBlur: true});
			passwordVerify.add(Validate.Presence, { failureMessage: "请输入密码"} );
	});

	 function submitLogin(form){
		 var a = LiveValidation.massValidate( [ emailVerify, passwordVerify] );
		 var invalidSize = $('.LV_invalid_field').size();
		 if(invalidSize>0){
			 return;
		 }
		 var pwd = $('#password').val();
		 pwd = $.md5(pwd);
		 $('#password').val(pwd);
		 ajaxLogin(form);
	 }

	 function ajaxLogin(form) {
		 	var email = $('#email').val();
		 	var password = $('#password').val();
		 	var keepLogin = ($('#keepLogin').attr("checked") == "checked");
		 	$.ajax({ 
		          type: "POST", 
		          url: contextPath + "/ajaxLogin", 
		          data:"user.email="+email + "&user.password=" + password + "&keepLogin=" + keepLogin, 
		          success: function(msg){ 
		 			msg = msg.replace("[","").replace("]","");
		 			var obj = $.parseJSON(msg);
		 			if(obj.loginSuccess == 1) {
		 				window.location.href = contextPath + "/viewArticle";
			 		} else if(obj.emailError == 1) {
		 				$('#email').addClass("LV_invalid_field");
		 				$('#email').next().removeClass("LV_valid");
		 				$('#email').next().addClass("LV_invalid");
		 				$('#email').next().text("无此注册邮箱");
						
			 		} else if(obj.passwordError == 1) {
			 			$('#password').addClass("LV_invalid_field");
		 				$('#password').next().removeClass("LV_valid");
		 				$('#password').next().addClass("LV_invalid");
		 				$('#password').next().text("密码错误");
				 	}
			 		
		         } 
		     }); 
		 }
</script>
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/css/validation.css" />
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/css/account.css" />
</head>
<body>
  <div class="wrapper">
  <div class="header-bar">
  <div class="header content clearfix">
  	<button onclick="window.location.href='<%=request.getContextPath()%>/viewRegister'" type="button" class="btn-info" style=" margin: 20px 20px 20px 20px;height: 32px; width:100px;font-size: 13px;float: right;">注册</button>
  </div>
  </div>
  <div class="main content clearfix">
  <div class="sign-in">
<div class="signin-box">
  <h2>登录 </h2>
  <form  id="loginform" action="<%=request.getContextPath()%>/login" method="post">
<div class="email-div">
  <label for="Email"><strong class="email-label">电子邮箱</strong></label>
  <input type="text" name="user.email" id="email" value="${user.email}">
</div>
<div class="passwd-div">
  <label for="Passwd"><strong class="passwd-label">密码</strong></label>
  <input type="password" name="user.password" id="password">
</div>
<div class="addition-div">
  <input type="checkbox" checked="checked" name="keepLogin" id="keepLogin">保持登录状态&nbsp;&nbsp;&nbsp;&nbsp;
  <a href="<%=request.getContextPath()%>/viewApplyPswReset">忘记密码</a>
  
</div>
  <button type="button" class="btn btn-danger" onclick="submitLogin(this.form)">登录</button>
</form>
  <div>
	<c:forEach items="${errorList}" var="item">
		<span class="invalid_msg" style="text-align: center">${item}</span>
	</c:forEach>
</div>
</div>
  </div>
  <div class="product-info ">
<div class="product-headers">
  <h1 class="redtext">帐户</h1>
</div>
<p>登录帐户，享受 更多服务。
</p><p>在右侧登录，或 <a href="<%=request.getContextPath()%>/viewRegister"> 免费创建帐户</a>。</p>
  </div>
  </div>
  </div>
</body>
</html>